package com.bfly.mall.order.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.common.util.DataConvertUtils;
import com.bfly.common.util.DateUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.mall.order.entity.OrderRefund;
import com.bfly.mall.order.dto.OrderRefundFormDTO;
import com.bfly.mall.order.dto.OrderRefundListDTO;
import com.bfly.mall.order.dto.OrderStatusDTO;
import com.bfly.mall.order.service.IOrderRefundService;
import com.bfly.mall.order.service.IOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 订单Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/4/19 9:23
 */
@RestController
@RequestMapping(value = "/manage/refund")
public class OrderRefundController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderRefundService orderRefundService;

    /**
     * 退款申请单列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/19 9:48
     */
    @GetMapping(value = "/list")
    public void listOrder(HttpServletRequest request, HttpServletResponse response) {
        PagerThreadLocal.set(request);
        Map<String, Object> param = new HashMap<>();
        String statusStr = request.getParameter("status");
        if (StringUtils.isNotBlank(statusStr)) {
            int status = DataConvertUtils.convertToInteger(statusStr);
            param.put("status", status);
        }

        String orderNo = request.getParameter("orderNo");
        if (StringUtils.isNotBlank(orderNo)) {
            param.put("order_no", orderNo);
        }
        String begin = request.getParameter("beginTime");
        String end = request.getParameter("endTime");
        if (StringUtils.isNotBlank(begin) && StringUtils.isNotBlank(end)) {
            Date beginDate = DateUtil.getDayMorning(DateUtil.parseStrDate(begin));
            Date endDate = DateUtil.getDayNight(DateUtil.parseStrDate(end));

            param.put("create_date_beginDate", beginDate);
            param.put("create_date_endDate", endDate);
        }
        Pager<OrderRefund> pager = orderRefundService.getPage(param);
        Pager<OrderRefundListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());
        if (pager.getData() != null) {
            List<OrderRefundListDTO> list = new ArrayList<>();
            OrderStatusDTO statusDTO;
            for (OrderRefund refund : pager.getData()) {
                OrderRefundListDTO dto = new OrderRefundListDTO();
                BeanUtils.copyProperties(refund, dto);

                statusDTO = orderService.getStatuses(refund.getOrderNo());
                BeanUtils.copyProperties(statusDTO, dto);

                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 退款操作
     * @author 长沙商脉科技有限公司
     * @date 2020/10/8 21:56
     */
    @PostMapping(value = "/process")
    @ActionModel(value = "退款")
    public void processRefundOrder(@Valid @RequestBody OrderRefundFormDTO formDTO, HttpServletResponse response, BindingResult result) {
        validData(result);
        orderRefundService.shopKeeperProcessRefundOrder(getAdmin(),formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 同意退款操作
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/25 18:22
     */
    @GetMapping(value = "/agree")
    @ActionModel(value = "同意退款/退货申请")
    public void agreeRefundOrder(@RequestParam("refundNo") String refundNo, HttpServletResponse response) {
        orderRefundService.shopKeeperAgreeRefundOrder(getAdmin(),refundNo);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 驳回退款申请操作
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/25 19:32
     */
    @GetMapping(value = "/reject")
    @ActionModel(value = "驳回退款/退货申请")
    public void rejectRefundOrder(@RequestParam("refundNo") String refundNo, @RequestParam("remark") String remark, HttpServletResponse response) {
        orderRefundService.shopKeeperRejectRefundOrder(getAdmin(),refundNo, remark);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}